//
//  SaleGoodsVC.m
//  jingdongfang
//
//  Created by MTiMac02 on 2018/1/9.
//  Copyright © 2018年 LJL. All rights reserved.
//

#import "SaleGoodsVC.h"
#import "MyGoodsCell.h"
#import "IGGetGoodsApi.h"
#import "IGDownMarketApi.h"
#import "IGMyGoods.h"
#import "GoodsDetailVC.h"

@interface SaleGoodsVC ()<UICollectionViewDelegate,UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
{
    UICollectionView *collect;
}
@property (nonatomic, strong) IGGetGoodsApi *saleGoodsApi;
@property (nonatomic, strong) IGDownMarketApi*downGoodsApi;
@property (nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic, copy) NSString *pageNum;
@property (nonatomic, copy) NSString *pageSize;
@property (nonatomic, copy) NSString *status;
@end

@implementation SaleGoodsVC
#pragma mark -懒加载
-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr=[NSMutableArray array];
    }
    return _dataArr;
}
#pragma mark -生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = RGB(234, 234, 234);
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing =3;
    layout.minimumInteritemSpacing = 3;
    
    collect = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, self.view.frame.size.height) collectionViewLayout:layout];
    collect.contentInset=UIEdgeInsetsMake(0, 0, NavigationMagin+41, 0);
    collect.backgroundColor = [UIColor clearColor];
    collect.delegate = self;
    collect.dataSource = self;
    collect.alwaysBounceVertical = YES;
    [collect registerClass:[MyGoodsCell class] forCellWithReuseIdentifier:@"MygoodsCell"];
    [self.view addSubview:collect];
    self.saleGoodsApi = [IGGetGoodsApi new];
    [self firstPage];
    @KWeakObj(self)
    [collect ig_setHeaderWithRefreshingBlock:^{
        [selfWeak firstPage];
    }];
}
- (void)firstPage
{
    _status=@"2";
    _pageSize = @"10";
    _pageNum = @"1";
    [self requestDate];
}

-(void)requestDate{
    self.saleGoodsApi.pageNum=_pageNum;
    self.saleGoodsApi.pageSize=_pageSize;
    self.saleGoodsApi.status=_status;
    @KWeakObj(self)
    [self.saleGoodsApi startWithDecodeClass:[IGGetGoodsResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        [collect ig_endHeaderRefreshing];
        [collect ig_endFooterRefreshing];
        if (responseObject) {
            IGGetGoodsResp *resp=responseObject;
            IGGetGoodsModel *model=resp.data;
            if (model.goodsList&&model.goodsList.count>0) {
                [selfWeak setupFooter];
            }
            else
            {
                [collect ig_endFooterRefreshingWithNoMoreData];
            }
            if ([_pageNum isEqualToString:@"1"])
            {
                _dataArr = [NSMutableArray arrayWithArray:model.goodsList];
            }
            else
            {
                [_dataArr addObjectsFromArray:model.goodsList];
            }
            
            [collect reloadData];
        }
        else
        {
            //空白页处理逻辑写在这里
        }
    }];
}
- (void)setupFooter
{
    @KWeakObj(self)
    [collect ig_setFooterWithRefreshingBlock:^{
        _pageNum = [NSString stringWithFormat:@"%ld", [_pageNum integerValue] + 1];
        [selfWeak requestDate];
    }];
}
- (void)scrollToTOP
{
    [collect setContentOffset:CGPointMake(0, 0) animated:YES];
}
#pragma mark----UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArr.count>0?self.dataArr.count:0;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MyGoodsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MygoodsCell" forIndexPath:indexPath];
     cell.data=_dataArr[indexPath.item];
    IGMyGoods *goods=self.dataArr[indexPath.item];
    __weak typeof(self) weakSelf = self;
    cell.soldGoods = ^{
        weakSelf.downGoodsApi=[[IGDownMarketApi alloc]init];
        self.downGoodsApi.gId=goods.gId;
        [weakSelf.downGoodsApi startWithDecodeClass:[IGDownMarketResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
            if (responseObject)
            {
                [self presentMessageTips:@"下架成功"];
                [collect ig_beginHeaderRefreshing];
//              [self.dataArr removeObjectAtIndex:indexPath.item];
//              [collect deleteItemsAtIndexPaths:@[indexPath]];
            }
        }];
    };
    [cell setNeedsLayout];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
            GoodsDetailVC *vc = [[GoodsDetailVC alloc] init];
            IGMyGoods *saleGoods=_dataArr[indexPath.item];
            vc.goodsId=saleGoods.gId;
            [self.navigationController pushViewController:vc animated:YES];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((KSCREENWIDTH-15)/2,270*KSCREENWIDTH/375);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(3, 6, 3, 6);
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
